package com.song.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * @Author Klys
 * @Date 2023/6/5 9:56
 * @Description
 * @Version 1.0
 */
@Configuration
public class SecurityUserConfig {


    //UserDetailsService只有loadUserByUsername(String username),方法返回一个UserDeails用户对象
    @Bean
    public UserDetailsService userDetailsService() {

        //创建两个对象
        UserDetails user1 = User.builder()
                .username("klys")
                .password(passwordEncoder().encode("123456"))
                .roles("student")   //权限转变为 ROLE_student
                .authorities("add", "update")
                .build();
        UserDetails user2 = User.builder()
                .username("root")
                .password(passwordEncoder().encode("123456"))
                .roles("teacher")
                .build();

        //基于内存的方式 创建两个用户对象
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser(user1);
        manager.createUser(user2);

        return manager;
    }

    /**
     * 自定义用户必须配置密码编译器
     **/
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
